<?php

namespace App\Http\Admin\Controller;

use App\Http\Admin\Middleware\AuthMiddleware;
use App\Http\Common\Controller\AbstractController;
use App\Http\Common\Result;
use App\Request\auth\Service\Common\UploadService;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use League\Flysystem\FilesystemException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

#[Middlewares([
    AuthMiddleware::class,
])]
#[Controller]
class CommonController extends AbstractController
{

    public function __construct(
        protected readonly UploadService $service,

    )
    {
    }

    #[GetMapping('/adminapi/common/dashboard')]
    public function dashboard(): Result
    {

        return $this->success();
    }


    /**
     * 文件上传
     * @param RequestInterface $request
     * @return Result
     * @throws FilesystemException
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping("/adminapi/common/upload")]
    public function upload(RequestInterface $request): Result
    {
        //获取当前域名以及协议
        $file = $request->file('file');
        $upload = $this->service->uploadFile($file);
        return $this->success($upload);
    }
}